<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2019/3/27
 * Time: 13:18
 */

namespace App\Http\Helper;

use EasyWeChat\Support\Arr;
use TestingPartRecordService;
use TestingPartService;
use TestingConfig;

class ResultHelper
{
    /**
     * 获取测评结果信息
     * @param $func
     * @param $uid
     * @param $tid
     * @return array
     */
    public static function dimensionResult($func, $uid, $tid) {
        $partRecord = TestingPartRecordService::getLastOneByUid($uid, $tid);
        $maxDimension = TestingPartService::getMaxDimensionByTid($tid);

        if ($partRecord['dimension'] != $maxDimension) {
            return [false, 'five_dimension.dimension_error'];
        }

        return [true, self::$func($partRecord, $uid, $tid)];
    }

    /**
     * 后端形体测评的报告 Http/Helper/ResultHelper.php:32 这里调用
     * @param $partRecord
     * @param $uid
     * @param $tid
     * @return array
     * @throws \App\Exceptions\UserException
     */
    private static function blackPhysiqueResult($partRecord, $uid, $tid) {
        $resultData = json_decode($partRecord['result'], true);
        $answer = formatArrValue($resultData, 'answer', []);

        $data = [];
        //智美形体评估报告，关键字输出解析
        $keywordOut = array_merge(Constant::$keyword_out, [ 'answer' => $answer ]);
        foreach (Constant::$physique_report_output as $key => $value) {
            $txtData = [];
            //把每段话提取出来解析
            foreach ($value as $item) {
                $fillData = [];
                if (!is_array($item)) continue;

                if (!isset($item['fill'])) {
                    var_dump($item);exit;
                }
                //解析需要填充的参数
                foreach ($item['fill'] as $val) {
                    if (is_array($val)) {
                        array_push($fillData, self::calcDifference($keywordOut, $val, $answer['height']['answer']));
                    } else {
                        $checkKeys = explode('.', $val);
                        foreach ($checkKeys as $cKey => $v) {
                            if (strpos($v, '[') !== false) {
                                $checkKeys[$cKey] = Arr::get($keywordOut, str_replace('-', '.', trim($v, '[]')));
                            }
                        }

                        array_push($fillData, Arr::get($keywordOut, implode('.', $checkKeys)));
                    }
                }

                array_push($txtData, my_sprintf($item['text'], $fillData));
            }

            //保存输出的数据信息
            $data[$key] = [
                'title' => $value['title'],
                'text' => $txtData
            ];
        }
        return $data;
    }

    /**
     * 智美五维测评 Http/Helper/ResultHelper.php:32 这里调用
     * @param $partRecord
     * @param $uid
     * @param $tid
     * @return array
     */
    private static function zmDimensionResult($partRecord, $uid, $tid) {
        $resultData = json_decode($partRecord['result'], true);
        $answer = formatArrValue($resultData, 'answer', []);

        if (isset($answer['gender'])) { //todo 兼容以前的，过了段时间就删了吧
            $stature = formatArrValue($answer, 'stature', 'A');
            $gender = formatArrValue($answer, 'gender', 'A');
        } else {
            $gender = $answer['sex']['weight'];
            $stature = $answer['stature']['weight'];
        }
        $zmStatureConfig = json_decode(TestingConfig::getValue('five.dimension.zm.stature', '{}'), true);

        return [
            'score' => formatArrValue($resultData, 'score', 0),
            'img' => $zmStatureConfig['img'][$gender][$stature],
            'title' => $zmStatureConfig['title'][$stature],
            'stature' => $zmStatureConfig['stature'][$stature],
            'diet' => $zmStatureConfig['diet'][$stature],
        ];
    }

    /**
     * 智美的形体测评 Http/Helper/ResultHelper.php:32 这里调用
     * @param $partRecord
     * @param $uid
     * @param $tid
     * @return array
     */
    private static function zmPhysiqueResult($partRecord, $uid, $tid) {
        $resultData = json_decode($partRecord['result'], true);
        $answer = formatArrValue($resultData, 'answer', []);
        $stature = formatArrValue($answer, 'stature', 'A');
        $gender = formatArrValue($answer, 'gender', 'A');

        $zmStatureConfig = json_decode(TestingConfig::getValue('five.dimension.zm.stature', '{}'), true);

        return [
            'score' => formatArrValue($resultData, 'score', 0),
            'img' => $zmStatureConfig['img'][$gender][$stature],
            'title' => $zmStatureConfig['title'][$stature],
            'stature' => $zmStatureConfig['stature'][$stature],
            'diet' => $zmStatureConfig['diet'][$stature],
        ];
    }

    /**
     * 计算标准的差值
     * @param $arr
     * @param $val
     * @param $height
     * @return int
     */
    private static function calcDifference($arr, $val, $height) {
        $data = Constant::$physique_standard[$val[0]];
        $input = Arr::get($arr, $val[1]);
        foreach ($data as $value) {
            if ($value['height'][0] <= $height && $height < $value['height'][1]) {
                if ($value['data'][0] > $input) {
                    return '+' . ($value['data'][0] - $input);
                } elseif ($value['data'][1] < $input) {
                    return '-' . ($input - $value['data'][1]);
                }
            }
        }
        return '0';
    }
}